package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategorySaveDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.service.CategoryService;
import com.sky.vo.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public CategoryVO page(CategoryDTO categoryDTO) {

        PageHelper.startPage(categoryDTO.getPage(),categoryDTO.getPageSize());
        Page page = categoryMapper.page(categoryDTO);
        long total = page.getTotal();
        List result = page.getResult();

        return new CategoryVO(total,result);
    }

    @Override
    public void save(CategorySaveDTO categorysaveDTO) {
//                .createTime(LocalDateTime.now())
//                .updateTime(LocalDateTime.now())
//                .createUser(BaseContext.getCurrentId())
//                .updateUser(BaseContext.getCurrentId())
        Category category = Category.builder().status(StatusConstant.ENABLE)
                .build();

        BeanUtils.copyProperties(categorysaveDTO,category);

        categoryMapper.save(category);

    }

    @Override
    public void update(CategorySaveDTO categorysaveDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categorysaveDTO,category);
        log.info("新增分类的：{}",category);
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public void delete(String id) {
        categoryMapper.delete(id);
    }

    @Override
    public void status(String status, String id) {
        categoryMapper.status(status,id);
    }

    @Override
    public List<Category> list(String type) {
        List list = categoryMapper.list(type);
        return list;
    }
}
